<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="录取专业方向" prop="stuName">
        <el-select v-model="queryParams.subId" placeholder="录取专业方向" clearable @keyup.enter.native="handleQuery"
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in subjectList" :key="index" :label="item.subName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选择导师专业方向" prop="stuName">
        <el-select v-model="queryParams.chooseSubId" placeholder="选择导师专业方向" clearable @keyup.enter.native="handleQuery"
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in subjectList" :key="index" :label="item.subName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="性别" prop="stuGender">
        <el-select v-model="queryParams.stuGender" placeholder="请输入性别" clearable @keyup.enter.native="handleQuery"
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in stuGenderOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>



      <el-form-item style="margin-left: 70px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="学号" align="center" prop="stuSno" />-->
      <el-table-column label="姓名" align="center" prop="stuName" />
      <el-table-column label="性别" align="center" prop="stuGender" >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.stuGender==0">女</el-tag>
          <el-tag v-if="scope.row.stuGender==1">男</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="录取专业方向" align="center" prop="subId" width="300">
        <template slot-scope="scope">
          <el-tag type="warning">{{subjectMap[scope.row.subId]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="选择导师专业方向" align="center" prop="chooseSubId" width="300" >
        <template slot-scope="scope">
          <el-tag type="warning">{{subjectMap[scope.row.chooseSubId]}}</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="考生号" align="center" prop="stuExamId" />-->
<!--      <el-table-column label="证件号码" align="center" prop="stuCardId" />-->
      <el-table-column label="出生日期" align="center" prop="stuBirth" />
<!--      <el-table-column label="毕业年月" align="center" prop="stuGraduateDate" />-->
      <el-table-column label="选择状态" align="center" prop="insExamineStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.insExamineStatus == 1" type="warning">等待导师确认</el-tag>
          <el-tag v-if="scope.row.insExamineStatus == 2" type="success">已确认</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="200px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.insExamineStatus==1"
            size="small"
            type="success"
            icon="el-icon-check"
            @click="insExamine(scope.row.id,2,scope.row.subId)"
            :disabled="!roundInfo.isSelect"
          >通过</el-button>
          <el-button
            v-if="scope.row.insExamineStatus==1"
            size="small"
            type="danger"
            icon="el-icon-close"
            @click="insExamine(scope.row.id,3,scope.row.subId)"
            :disabled="!roundInfo.isSelect"
          >不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="toStudentInfo(scope.row.id)"
          >查看详细信息</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
  import { exportStudent,exportStudentByInsId } from "@/api/mentor/student";
  import { getAllSubject } from "@/api/mentor/subject";
  import {getInstructorDetailByUserId} from "@/api/mentor/instructor";
  import {getInfo} from "@/api/login";
  import {getMyStudent,insExamine} from '@/api/mentor/choose'
  import {getAllSubjectMap} from "../../../api/mentor/subject";
  import {isSelect} from "../../../api/mentor/round";

  export default {
    name: "studentList",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 研究生信息表格数据
        studentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          subId: undefined,
          stuName: undefined,
          stuGender: undefined,
          chooseSubId:undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {

        },
        stuGenderOptions: [{
          "label": "女",
          "value": 0
        }, {
          "label": "男",
          "value": 1
        }],
        subjectList:[],
        subjectMap:{},
        instructorInfo:{},
        user:{},
        roundInfo:{
          isSelect:undefined,
          msg:undefined
        }
      };
    },
    async created() {

      await this.getInstructorInfo()


      var flag = this.checkUser(this.user)
      if (!flag){
        this.$message.warning("本菜单只对导师开放")
        return this.$router.push("/")
      }
      //如果当前学生信息未通过审核，跳转到个人中心页面
      if (this.instructorInfo.examineStatus!=3) {
        await this.$alert(`个人信息未通过审核`, "提示", {
          confirmButtonText: '确定',
          dangerouslyUseHTMLString: true,
          callback: action => {
            return this.$router.push('/instructor/center')
          }
        })
      }

      await this.getAllSubject()
      await this.getSubjectMap()
      await this.getList()
      await this.getInstructorSelectTime()
    },
    methods: {
      async getList(){
        this.loading = true;
        this.queryParams.insId = this.instructorInfo.insId
        const res = await getMyStudent(this.queryParams)
        // console.log(res)
        this.studentList = res.rows;
        this.total = res.total;
        this.loading = false;
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {}
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有研究生信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          return exportStudentByInsId(this.instructorInfo.insId);
        }).then(response => {
          this.download(response.msg);
        })
      },
      toStudentInfo(studentId){
        //跳转到学生详细信息页面
        // console.log(111)
        this.$router.push(`/student/detail/${studentId}`)
      },
      async getAllSubject() {
        const res = await getAllSubject()
        this.subjectList = res.data
        // console.log(res)
      },
      async getSubjectMap() {
        const res = await getAllSubjectMap()
        this.subjectMap = res.data
        // console.log(res)
      },
      async getInstructorInfo() {
        const info = await getInfo()
        this.user = info.user
        const res = await getInstructorDetailByUserId(this.user.userId)
        this.instructorInfo = res.data
      },
      checkUser(user){
        // console.log(user)
        for (let i = 0; i < user.roles.length; i++) {
          if (user.roles[i].roleKey=='instructor'){
            return true
          }
        }
        return false
      },
      //导师审核
      async insExamine(stuId,status,subId){

        const confirm = await this.$confirm(`确认审核${status==2?'通过':'不通过?'}`,'提示',{}).catch(err=>err)

        if (confirm=='cancel'){
          return this.$message.success('取消成功')
        }
        const res = await insExamine(stuId,this.instructorInfo.insId,subId,status)
        await this.getList()
      },
      // 获取当前导师的选择时间
      async getInstructorSelectTime() {
        const res = await isSelect(1);
        if (res.data){
          this.roundInfo.isSelect = res.data.isSelect
          this.roundInfo.msg = res.data.insStart+"——"+res.data.insEnd
          if (!res.data.isSelect){
            await this.$alert(`当前选择时间段${this.roundInfo.msg}`, "未到选择时间", {
              confirmButtonText: '确定',
              dangerouslyUseHTMLString: true,
            })
          }else {
            //
            await this.$alert(`<div>当前选择时间段:</div><div>${this.roundInfo.msg}</div>`, {
              confirmButtonText: '确定',
              dangerouslyUseHTMLString: true
            })
          }
        }else {
          this.roundInfo.isSelect = false
          this.roundInfo.msg = "当前系统未开放选择时间"
          await this.$alert(``,"系统未开放选择时间", {
            confirmButtonText: '确定',
            dangerouslyUseHTMLString: true
          })
        }




      }

    }
  };
</script>
